package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IAlertUserServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.AlertUserAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.AlertUserAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.AlertUserUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.AlertUserAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.AlertUserPageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.AlertUser;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.AlertUserEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.AlertUserQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.AlertUserService;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.support.ApplicationNameHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;

@RestController
public class AlertUserServiceRemoteImpl implements IAlertUserServiceRemote {
    @Autowired
    private AlertUserService alertUserService;

    @Override
    public ResponseData<AlertUserPageAResp> list(String likeUsername, String likeComment, Integer page, Integer size) {
        ResponseData<AlertUserPageAResp> responseData = new ResponseData<>();
        if (page == null || page == 0) {
            page = 1;
        }
        if (size == null || size == 0) {//如果是空的默认查询全部
            size = Integer.MAX_VALUE;
        }

        AlertUserQuery alertUserQuery = new AlertUserQuery();
        alertUserQuery.setApplicationNames(Lists.newArrayList(ApplicationNameHolder.get()));
        if (Detect.notEmpty(likeUsername)) {
            alertUserQuery.setLikeUsername(URLDecoder.decode(likeUsername));
        }
        if (Detect.notEmpty(likeComment)) {
            alertUserQuery.setLikeComment(URLDecoder.decode(likeComment));
        }

        Pagination<AlertUserEnhanced> pagination = new Pagination<>(page, size, true);
        pagination.addOrder(new Order(AlertUser.COLUMN_CREATE_TIME, Order.ORDER_DESC), AlertUser.class);

        pagination = alertUserService.getPagination(alertUserQuery, pagination, null);

        AlertUserPageAResp alertUserPageAResp = new AlertUserPageAResp();
        alertUserPageAResp.setCount(pagination.getCount());
        alertUserPageAResp.setPage(pagination.getIndex());
        alertUserPageAResp.setSize(pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            alertUserPageAResp.setItems(CommonConvent.convent2AlertUserAResps(pagination.getItems()));
        }

        responseData.setData(alertUserPageAResp);

        return responseData;
    }

    @Override
    public ResponseData<AlertUserAResp> detail(@RequestBody RequestData<AlertUserAReq> requestData) {
        ResponseData<AlertUserAResp> responseData = new ResponseData<>();

        responseData.setData(CommonConvent.convent2AlertUserAResp(alertUserService.getEnhanced(requestData.getRequest().getId(), null)));

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<AlertUserUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();
        AlertUserUpdateAReq alertUserUpdateAReq = requestData.getRequest();

        alertUserService.updateAlertUser(alertUserUpdateAReq.getId(), alertUserUpdateAReq.getUsername(), alertUserUpdateAReq.getComment(), alertUserUpdateAReq.getPhoneNo(), alertUserUpdateAReq.getEmail(), null);

        return responseData;
    }

    @Override
    public ResponseData<AlertUserAResp> add(@RequestBody RequestData<AlertUserAddAReq> requestData) {
        ResponseData<AlertUserAResp> responseData = new ResponseData<>();

        AlertUserAddAReq alertUserAddAReq = requestData.getRequest();
        AlertUser alertUser = new AlertUser();
        BeanUtils.copyProperties(alertUserAddAReq, alertUser);
        alertUser.setApplicationName(ApplicationNameHolder.get());

        alertUser = alertUserService.saveAlertUser(alertUser, null);

        responseData.setData(CommonConvent.convent2AlertUserAResp(alertUserService.convent2Enhanced(alertUser)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") Integer id) {
        ResponseData responseData = new ResponseData();

        alertUserService.deleteAlertUser(id, null);

        return responseData;
    }
}
